![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@polkadot/api
Advanced tools
The Polkadot-JS API provides easy-to-use wrappers around JSONRPC calls that flow from an application to a node. It handles all the encoding and decoding or parameters, provides access to RPC functions and allows for the query of chain state and the submission of transactions.
The API wrappers provide a standard interface for use -
.create(<optional ApiOptions>)
that returns an API instance when connected, decorated and ready-to use. ApiOptions can include an optional WsProvider and optional custom type definitions { provider: <Optional WsProvider>, types: <Optional RegistryTypes> }
.new Api(<optional ApiOptions>)
, exposing the isReady
getterapi.rpc.<section>.<method>
provides access to actual RPC calls, be it for queries, submission or retrieving chain information
api.query.<section>.<method>
provides access to chain state queries. These are dynamically populated based on what the runtime provides
api.tx.<section>.<method>
provides the ability to create a transaction, like chain state, this list is populated from a runtime query
api.consts.<section>.<constant>
provides access to the module constants (parameter types).
There are two flavours of the API provided, one allowing a standard interface via JavaScript Promises and the second provides an Observable wrapper using RxJS. Depending on your use-case and familiarity, you can choose either (or even both) for your application.
.create(...)
. Additionally any subscription method uses (value) => {}
callbacks, returning the value as the subscription is updated..create(...)
. In the same fashion subscription-based methods return long-running Observables that update with the latest values.Substrate (upon which Polkadot is built) uses on-chain WASM runtimes, allowing for upgradability. Each runtime defining the actual chain extrinsics (submitted transactions and block intrinsics) as well as available entries in the chain state. Due to this, the API endpoints for queries and transactions are dynamically populated from the running chain.
Due to this dynamic nature, this API departs from traditional APIs which only has fixed endpoints, driving use by what is available by the runtime. As a start, this generic nature has a learning curve, although the provided documentation, examples and linked documentation tries to make that experience as seamless as possible.
Installation -
npm install --save @polkadot/api
Subscribing to blocks via Promise-based API -
import { ApiPromise } from '@polkadot/api';
// initialise via static create
const api = await ApiPromise.create();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads((header) => {
console.log(`Chain is at #${header.number}`);
});
Subscribing to blocks via RxJS-based API -
import { ApiRx } from '@polkadot/api';
// initialise via static create
const api = await ApiRx.create().toPromise();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads().subscribe((header) => {
console.log(`Chain is at #${header.number}`);
});
Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime.
import { ApiPromise } from '@polkadot/api';
// initialise via static create and register custom types
const api = await ApiPromise.create({
types: {
CustomTypesExample: {
"id": "u32",
"data": "Vec<u8>",
"deposit": "Balance",
"owner": "AccountId",
"application_expiry": "Moment",
"whitelisted": "bool",
"challenge_id": "u32"
}
}
});
Some of the users of the API (let us know if you are missing from the list), include -
9.14.2 Feb 19, 2023
Changes:
WsProvider.connect()
on an open connection (creates resource leaks)FAQs
Promise and RxJS wrappers around the Polkadot JS RPC
The npm package @polkadot/api receives a total of 93,282 weekly downloads. As such, @polkadot/api popularity was classified as popular.
We found that @polkadot/api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.